﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TroopFormationPivot : BattleComponent<TroopFormationPivot>
{
    Ticker formationUpdateTicker = new Ticker(2, true);

    public TroopLeader leader;
    public float FormationArriveThreshold;
    public float arriveDistance;
    
    public bool DrawGizmos;

    public Formation.FormationBase formation;

    
    private void Awake()
    {
        formation = new Formation.LineFormation(15, 3, 3, arriveDistance);
    }

    private void OnEnable()
    {
        Assign(this);
    }

    private void OnDisable()
    {
        Unassign(this);
    }

    private void OnDrawGizmos()
    {
        if (DrawGizmos)
        {
            if (formation.UnitSlotList != null)
            {
                foreach (var fu in formation.UnitSlotList)
                {
                    Gizmos.DrawSphere(fu.Key.position, 1);
                }
            }
        }
    }

    public static void UpdateTroopFormation()
    {
        foreach (var pivot in ComponentDataList)
        {
            if (pivot.formationUpdateTicker.Tick())
            {
                if ( pivot.leader.troopState == TroopState.FormationMoving )
                {
                    pivot.formation.UpdateLineFormationSlotPosAndRot(pivot.transform);
                    pivot.formation.UpdateUnitTarget();
                }
            }
        }
    }

}
